#include "main.h"

#include "spi.h"
#include "app_lvgl.h"
#include "app_screen.h"
#include "uart.h"
#include "lcd.h"
#include "lcd_lvgl_config.h"
#include "mynes_config.h"
#include "event.h"
#include "nes.h"

void app_main()
{

  SPI_init();
  MCP23S08_init();

  char *buf =(char*)heap_caps_malloc(1024*1, MALLOC_CAP_SPIRAM);

  if(buf==NULL)
  {
    ESP_LOGI("app_main","内存申请失败\r\n"); 
  }

  ESP_LOGI("app_main","-------------开始运行----------------\r\n"); 
  ESP_LOGI("app_main","esp32 sdk version :%s\n", esp_get_idf_version());  
  


  SYSTEM_State.nvs_sta = nvs_init();
  SYSTEM_State.spiffs_sta = myspiffs_init();
  SYSTEM_State.sd_sta = sd_card_init();
  // nvs_set_wifi("ASKJ","12345678");
  // nvs_get_wifi(SYSTEM_State.wifi_name,SYSTEM_State.wifi_password);

  // LCD_init();
  // while (1) 
  // {

  //   vTaskDelay(100 / portTICK_PERIOD_MS); 
  // } 

  lcd_hal_init();
  IIC_Init();
  //gpio_out_Init();
  key_Init();
  UART_init();
  SYSTEM_State.sht20_sta = SHT2x_Init();
  SYSTEM_State.ft5206_sta = ft5206_init(); 
  SYSTEM_State.wm8978_sta = WM8978_Init();
 // SYSTEM_State.mpu6050_sta = mpu6050_init();

  // while (1) 
  // {

  //   vTaskDelay(100 / portTICK_PERIOD_MS); 
  // }  

  iis_init();
    
  //ft6336_init();
  // while (1) 
  // {

  //   vTaskDelay(100 / portTICK_PERIOD_MS); 
  // }  
	

  //mynes_init(ROM_FILE);

//  max17043_init();
//   while (1) 
//   {

//     vTaskDelay(100 / portTICK_PERIOD_MS); 
//   }  
	
/*  printf_memory();
  while (1) 
  {

    if (gpio_get_level(KEY_1)==0)
    {

      mynes_init(nes_file_4);      
    }

    if (gpio_get_level(KEY_2)==0)
    {
      mynes_init(nes_file_3);      
    }
    if (gpio_get_level(KEY_3)==0)
    {

      mynes_init(nes_file_2);
      
    }
    if (gpio_get_level(KEY_4)==0)
    {

      mynes_init(nes_file_1); 
    }    

    vTaskDelay(100 / portTICK_PERIOD_MS);   
  }
*/

  lv_init();
  lv_port_fs_init();
  //lcd_lvgl_init(); 
  app_lvgl_init();
  SYSTEM_INIT();
  wifi_init();
	// while (1) 
  // {
  //     vTaskDelay(5000 / portTICK_PERIOD_MS);   
  // }
}


void ptintf_neicun(char *file,int len)
{
    int free_size;

    ESP_LOGW("内存监控", "%s:%d",file,len);
    free_size = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
    printf("内部RAM剩余%d.%dKB  ", free_size/1024,free_size%1024);
    free_size = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
    printf("外部PSRAM剩余%d.%dkB\r\n",free_size/1024,free_size%1024);  
}

